package com.my.xq.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.my.xq.domain.ProprietorQueryParam;
import com.my.xq.domain.entity.Proprietor;
import com.my.xq.repository.ProprietorRepository;
import com.sskj.core.annotation.ControllerLogExeTime;
import com.sskj.core.util.JsonUtil;
import com.sskj.sys.domain.entity.MediaEntity;
import com.sskj.sys.repository.MediaRepository;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/xq/zuhu")
public class ZuHuServiceImpl {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	ProprietorRepository eRepository;

	@Autowired
	MediaRepository mediaRepository;

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/page")
	@ControllerLogExeTime(description = "查询租户信息", log = false)
	@ApiOperation(value = "查询租户信息", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "name", value = "姓名", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "pageNumber", value = "页号", paramType = "form", dataType = "int"),
			@ApiImplicitParam(name = "pageSize", value = "页的大小", paramType = "form", dataType = "int") })
	public Page<Proprietor> queryPage(@ApiIgnore ProprietorQueryParam param,
			@ApiIgnore Pageable pageable, HttpServletRequest request) {

		Page<Proprietor> page = eRepository.findPage(param, pageable);
		List<Proprietor> list = page.getContent();
		if (list != null) {
			for (Proprietor dg : list) {
				getUrlAndBfb(dg, request);
			}
		}
		return page;
	}

	public void getUrlAndBfb(Proprietor o, HttpServletRequest request) {
		List<MediaEntity> mes = mediaRepository.queryPic(o.getId());
		for (MediaEntity me : mes) {
			o.getUrlList().add(request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
					+ "/" + me.getHttpurl());
			System.out.println("测试");
			System.out.println(request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ "/" + me.getHttpurl());			
		}


		

	}

	@RequestMapping(value = "/update", method = { RequestMethod.POST, RequestMethod.PUT })
	@ControllerLogExeTime(description = "修改租户信息")
	public void update(Proprietor Proprietor) {
		String id = Proprietor.getId();
		Proprietor entity = eRepository.get(id);		
		entity.setName(Proprietor.getName());
		entity.setPhone(Proprietor.getPhone());
		entity.setPriType(Proprietor.getPriType());		
		logger.info(JsonUtil.json(entity));
		eRepository.save(entity);

				

	}
	
	private void updateMedia(String id,String files)
	{
		List<String> ids = new ArrayList<>(); 			
		if (!StringUtils.isEmpty(files)) {
			String[] as = files.split(",");
			for (int i = 0; i < as.length; i++) {
				ids.add(as[i]);
			}
		}				
		mediaRepository.updateMedia(id,ids);		
	}	
	
	private void delMedia(String id,String files)
	{
		List<String> ids = new ArrayList<>(); 			
		if (!StringUtils.isEmpty(files)) {
			String[] as = files.split(",");
			for (int i = 0; i < as.length; i++) {
				ids.add(as[i]);
			}
		}				
		mediaRepository.delMedia(id,ids);		
	}

	@RequestMapping(value = "/save", method = { RequestMethod.POST, RequestMethod.PUT })
	@ControllerLogExeTime(description = "新增租户信息")
	public Proprietor save(Proprietor Proprietor) {		
		eRepository.save(Proprietor);


		return Proprietor;
	}

	@ControllerLogExeTime(description = "删除租户信息")
	@RequestMapping("/del")
	@ApiOperation(value = "删除租户信息", notes = "删除租户信息 多个ID用,号分隔", httpMethod = "DELETE")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "租户IDS", paramType = "form", required = true, dataType = "string") })
	public void del(String id) {

		eRepository.delete(id);

		if (StringUtils.isNotEmpty(id)) {
			String[] idsArr = id.split(",");
			List<String> list = new ArrayList<String>();

			if (idsArr != null) {
				for (String idd : idsArr) {
					if (!idd.equals("ADMIN")) {
						list.add(idd);
					}
				}
				eRepository.delete(list);

			}
		}

	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@ControllerLogExeTime(description = "获取租户信息", log = false)
	@RequestMapping("/get")
	public Proprietor get(String id, HttpServletRequest request) {
		Proprietor Proprietor = eRepository.get(id);

		getUrlAndBfb(Proprietor, request);

		return Proprietor;
	}

}
